package com.szxy.item.service.impl;

import com.netflix.discovery.converters.Auto;
import com.szxy.item.service.ItemCategoryService;
import com.szxy.mapper.TbItemCatMapper;
import com.szxy.pojo.TbItemCat;
import com.szxy.pojo.TbItemCatExample;
import com.szxy.utils.CatNode;
import com.szxy.utils.CatResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther:zwer
 * @Date:2019/9/7 15:55
 * @Description:com.szxy.item.service.impl
 * @Version:1.0
 **/
@Service
public class ItemCategoryServiceImpl implements ItemCategoryService {

    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public List<TbItemCat> selectItemCategoryByParentId(Long id) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id); //where  parent_id = ?
        List<TbItemCat> itemCatList = this.itemCatMapper.selectByExample(example);
        return itemCatList;
    }

    @Override
    public TbItemCat findItemCatById(Long cid) {
        return this.itemCatMapper.selectByPrimaryKey(cid);
    }

    @Override
    public CatResult selectItemCategoryAll() {
        CatResult catResult = new CatResult();
        catResult.setData(getItemCateList(0L));
        System.out.println(catResult.getData().size());
        return catResult;
    }

    private List<?> getItemCateList(Long parentId){
        List dataList = new ArrayList<>();
        List<TbItemCat> itemCatList = selectItemCategoryByParentId(parentId);
        int count = 0;
        for (TbItemCat tbItemCat : itemCatList) {
            if(tbItemCat.getIsParent()){
                CatNode catNode = new CatNode();
                catNode.setName(tbItemCat.getName());
                catNode.setItem(getItemCateList(tbItemCat.getId()));
                dataList.add(catNode);
                count++;
                if(count == 18){
                    return dataList;
                }
            }else{
                dataList.add(tbItemCat.getName());
            }
        }
        return dataList;
    }
}
